package jm.nj.demo04mian.demo17;
/*
* 抽象类作为方法的参数和返回值类型
* */
public class Demo {
    public static void main(String[] args) {
        method(new Cat());
        method(new Dog());
        
        Animal a = getAnimal();
        a.sleep();
        a.eat();
        System.out.println("---------");
        Animal a2 = getAnimal(333);
        a2.eat();
        a2.sleep();
    }

    private static Animal getAnimal(int a) {
        if (a==0){
            return new Cat();
        }else if(a==1){
            return new Dog();
        }else{
            return new Animal() {
                @Override
                public void eat() {
                    System.out.println("动物c");
                }

                @Override
                public void sleep() {
                    System.out.println("动物s");
                }
            };
        }

    }

    private static Animal getAnimal() {
        return new Cat();
    }


    public static void method(Animal a) {
        a.eat();
        a.sleep();
        //多态无法使用子类特有成员
        if(a instanceof  Cat){
            Cat c = (Cat)a;
            c.catchMouse();
        }
    }
}

